package principles.P03_依赖倒转原则_DependencyInversion;

public class P02 {
    public static void main(String[] args) {

        //region 方法一
//        ChangHong changHong = new ChangHong();
//        OpenAndClose openAndClose = new OpenAndClose();
//        openAndClose.open(changHong);
        //endregion

        //region 方法二
//        ChangHong changHong = new ChangHong();
//        OpenAndClose openAndClose = new OpenAndClose(changHong);
//        openAndClose.open();
        //endregion

        //region 方法三
        ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setITV(changHong);
        openAndClose.open();
        //endregion
    }
}

//region 方法一：通过接口传递实现依赖
//interface IOpenAndClose {
//    public void open(ITV itv);
//}
//
//interface ITV {
//    public void play();
//}
//
//class ChangHong implements ITV {
//
//    @Override
//    public void play() {
//        System.out.println("长虹电视机打开了");
//    }
//}
//
//class OpenAndClose implements IOpenAndClose {
//
//    @Override
//    public void open(ITV itv) {
//        itv.play();
//    }
//}
//endregion

//region 方法二：通过构造方法依赖传递

//interface IOpenAndClose {
//    public void open();
//}
//
//interface ITV {
//    public void play();
//}
//
//class ChangHong implements ITV {
//
//    @Override
//    public void play() {
//        System.out.println("长虹电视机打开了");
//    }
//}
//
//class OpenAndClose implements IOpenAndClose {
//    private ITV itv;
//
//    public OpenAndClose(ITV itv) {
//        this.itv = itv;
//    }
//
//    @Override
//    public void open() {
//        itv.play();
//    }
//}
//endregion

//region 方法三：通过setter方法传递

interface IOpenAndClose {
    public void open();

    public void setITV(ITV itv);
}

interface ITV {
    public void play();
}

class ChangHong implements ITV {

    @Override
    public void play() {
        System.out.println("长虹电视机打开了");
    }
}

class OpenAndClose implements IOpenAndClose {
    private ITV itv;
    @Override
    public void open() {
        this.itv.play();
    }

    @Override
    public void setITV(ITV itv) {
        this.itv = itv;
    }
}
//endregion

